package com.rambar.weeklyweatherwidget.chart;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.RangeCategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import com.rambar.weeklyweatherwidget.weather.FutureWeatherData.WeatherData;
import com.rambar.weeklyweatherwidget.weather.WeatherDataManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.util.Log;
import android.view.View;

public class ChartRenderer {
	private static String TAG = "WWW";
	
	private Context mContext;
	private WeatherDataManager mWeatherDataManager;

	public ChartRenderer(Context context) {
		mContext = context;
		mWeatherDataManager = WeatherDataManager.getInstance(context);
	}

	protected void setChartSettings(XYMultipleSeriesRenderer renderer,
			String title, String xTitle, String yTitle, double xMin,
			double xMax, double yMin, double yMax, int axesColor,
			int labelsColor) {
		// renderer.setChartTitle(title);
		// renderer.setXTitle(xTitle);
		// renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
		renderer.setShowLegend(false);
		renderer.setShowAxes(false);
		renderer.setShowLabels(false);
		renderer.setShowGridX(true);
	}

	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		int length = colors.length;
		for (int i = 0; i < length; i++) {
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(colors[i]);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}

	public Bitmap render() {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		RangeCategorySeries series = new RangeCategorySeries("Temperature");
		double minDegree=999, maxDegree=-999;
		for (int i = 0; i < 6; i++) {
			WeatherData wd = mWeatherDataManager.getFutureWeatherData(i);
			if(wd.mMinTemp == -999 || wd.mMaxTemp == -999) {
				series.add(-999.0, -999.0);
				continue;
			}
			
			series.add((double)wd.mMinTemp, (double)wd.mMaxTemp);
			minDegree = Math.min(minDegree, (double)wd.mMinTemp);
			maxDegree = Math.max(maxDegree, (double)wd.mMaxTemp);
		}
		dataset.addSeries(series.toXYSeries());
		int[] colors = new int[] { Color.WHITE };
		XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		setChartSettings(renderer, "Monthly temperature range", "Month",
				"Celsius degrees", 0.55, 6.45, minDegree-5, maxDegree+5, Color.GRAY, Color.LTGRAY);
		renderer.setBarSpacing(0.5);
		renderer.setXLabels(0);
		renderer.setYLabels(10);
		renderer.setMargins(new int[] { 0, 0, 0, 0 });
		renderer.setYLabelsAlign(Align.RIGHT);
		SimpleSeriesRenderer r = renderer.getSeriesRendererAt(0);
		r.setDisplayChartValues(true);
		r.setChartValuesTextSize(20);
		r.setChartValuesSpacing(8);
		r.setGradientEnabled(true);
		r.setGradientStart(minDegree-5, Color.argb(0xFF, 0x8E, 0xB4, 0xE3));
		r.setGradientStop(maxDegree, Color.argb(0xFF, 0xFF, 0xC0, 0x00));

		GraphicalView chartView = ChartFactory.getRangeBarChartView(mContext,
				dataset, renderer, Type.STACKED);

		Bitmap bitmap = loadBitmapFromView(chartView, 650, 150);
		return bitmap;
	}

	public static Bitmap loadBitmapFromView(View v, int width, int height) {
		Bitmap b;

		v.layout(0, 0, width, height);
		b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
				Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(b);
		v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
		v.draw(c);

		return b;
	}
}


		